package com.qf.controller;

import com.qf.entity.SysUser;
import com.qf.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 2019/9/23
 * Administrator
 * ssm0923
 * 面向对象面向君  不负代码不负卿
 */
@Controller
public class UserController {

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/tologin")
    public String login(HttpServletRequest request) throws  Exception{
        //从FormAuthenticationFilter获得验证失败的错误信息
        String failure = (String)request.getAttribute("shiroLoginFailure");
        if(failure!=null){
            if(UnknownAccountException.class.getName().equals(failure)){
                throw new Exception("用户名错误");
            }else if(IncorrectCredentialsException.class.getName().equals(failure)){
                throw new Exception("密码错误");
            }else {
                throw new Exception("未知错误");
            }
        }
        return "login";
    }


    @RequestMapping("/getusers")
    public String aaa(HttpSession session){
        Subject subject = SecurityUtils.getSubject();
        SysUser user = (SysUser) subject.getPrincipal();
        session.setAttribute("user",user);
        System.out.println(session.getAttribute("user"));
        return "success";
    }



}
